package com.firefly.admin.common.handle;

import com.firefly.admin.common.utils.RedisUtil;
import com.firefly.admin.config.entity.FySystemConfiguration;
import com.firefly.admin.config.entity.FySystemDictionary;
import com.firefly.admin.config.entity.FySystemDictionaryItem;
import com.firefly.admin.system.entity.FySystemWhiteList;
import lombok.extern.slf4j.Slf4j;
import com.firefly.admin.common.constant.Constants;
import com.firefly.admin.config.service.FySystemConfigurationService;
import com.firefly.admin.config.service.FySystemDictionaryItemService;
import com.firefly.admin.config.service.FySystemDictionaryService;
import com.firefly.admin.system.service.FySystemWhiteListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class PreloadApplicationHandler implements ApplicationRunner {

    @Autowired
    private FySystemConfigurationService fySystemConfigurationService;
    @Autowired
    private FySystemDictionaryItemService fySystemDictionaryItemService;
    @Autowired
    private FySystemDictionaryService fySystemDictionaryService;
    @Autowired
    private FySystemWhiteListService fySystemWhiteListService;


    @Override
    public void run(ApplicationArguments args) {
        //项目加载时缓存配置项到redis
        List<FySystemConfiguration> configList = fySystemConfigurationService.getAllConfig();
        if (!CollectionUtils.isEmpty(configList)) {
            configList.forEach(stSystemConfiguration -> RedisUtil.hset(Constants.Redis.CONFIG_KEY, stSystemConfiguration.getConfigKey(), stSystemConfiguration.getConfigValue()));
        }

        //项目加载时缓存字典项到redis
        List<FySystemDictionary> dictionaryList = fySystemDictionaryService.findAllNormal();
        if (!CollectionUtils.isEmpty(dictionaryList)) {
            dictionaryList.forEach(this::cacheDictionary);
        }

        //项目加载时缓存系统白名单到redis
        List<FySystemWhiteList> whiteLists = fySystemWhiteListService.findAllNormal();
        if (!CollectionUtils.isEmpty(whiteLists)) {
            cacheWhiteList(whiteLists);
        }
    }

    public void cacheDictionary(FySystemDictionary fySystemDictionary) {
        List<FySystemDictionaryItem> itemList = fySystemDictionaryItemService.findByDictionaryId(fySystemDictionary.getDictionaryId());
        RedisUtil.hset(Constants.Redis.DICTION_KEY, fySystemDictionary.getDictionaryCode(), itemList);
    }

    public void cacheWhiteList(List<FySystemWhiteList> whiteLists) {
        Map<String, List<FySystemWhiteList>> map = whiteLists.stream().collect(Collectors.groupingBy(FySystemWhiteList::getWhiteListType));
        map.forEach((whiteListType, stSystemWhiteLists) -> {
            RedisUtil.hset(Constants.Redis.WHITE_LIST_KEY, whiteListType, stSystemWhiteLists);
        });
    }
}
